package Actions;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class updateStatusAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		forms.showappInfoform lo = (forms.showappInfoform) form;
		
		String url = "jdbc:postgresql://localhost:5432/Applications";
		Connection conn = null;
		Statement stmt = null;
		Statement stmt2 = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		ResultSet rs2 = null;
		String instruc = null;

		// Registering Postgresql JDBC driver
        try {
        	
			Class.forName("org.postgresql.Driver");
	        // Open a connection to the database        
	        conn = DriverManager.getConnection(url,"postgres","postgres");
	        // Create the statement
	        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
    					ResultSet.CONCUR_UPDATABLE);
	        stmt2 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
    					ResultSet.CONCUR_UPDATABLE);
	        
	        int appid = lo.getAppID();
	        rs = stmt.executeQuery("SELECT * FROM Application WHERE app_id = '"+appid+"'");
	        
			instruc = "UPDATE Application SET Status = '"+ request.getParameter("status")
			+"' WHERE app_id ='"+appid+"'";
			ps = conn.prepareStatement( instruc );
			ps.executeUpdate();
	        
			instruc = "UPDATE Application SET ready = 'YES'"
			+" WHERE app_id ='"+appid+"'";
			ps = conn.prepareStatement( instruc );
			ps.executeUpdate();
			
			instruc = "UPDATE Application SET rev_id = '0'"
				+" WHERE app_id ='"+appid+"'";
				ps = conn.prepareStatement( instruc );
				ps.executeUpdate();
			
			lo.reset();
	        
		}catch (SQLException e) 
		{ 
			System.out.println(e);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
			
		  return mapping.findForward("decided");
		
		
		}
}
